package cn.eoeit.mmy.service.impl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;










import cn.eoeit.mmy.bean.Pagination;
import cn.eoeit.mmy.dao.IBaseDao;
import cn.eoeit.mmy.model.FeedBackModel;
import cn.eoeit.mmy.model.RoleModel;
import cn.eoeit.mmy.model.UserInfo;
import cn.eoeit.mmy.service.IUserService;
import cn.eoeit.mmy.util.SysUtils;

@Service("userService")
public class UserServiceImpl implements IUserService {

	@Autowired
	private IBaseDao<UserInfo> baseDao;
	
	@Autowired
	private IBaseDao<RoleModel> roleDao;
	
	@Autowired
	private IBaseDao<FeedBackModel> feebackDao;

	public IBaseDao<UserInfo> getBaseDao() {
		return baseDao;
	}

	public void setBaseDao(IBaseDao<UserInfo> baseDao) {
		this.baseDao = baseDao;
	}

	

	@Override
	public List<UserInfo> getUsers() {
		return baseDao.find("from UserInfo");
	}

	

	@Override
	public UserInfo finishLogin(UserInfo user) {
		String hql="from UserInfo where userName=?";
		UserInfo  userinfo=baseDao.get(hql, user.getUserName());
		if(userinfo!=null){
			if(userinfo.getPassword().equals(SysUtils.string2MD5(user.getPassword()))){
				
				userinfo.setLastUpdateTime(System.currentTimeMillis());
				baseDao.update(userinfo);
				RoleModel role=roleDao.get(RoleModel.class, userinfo.getRoleID());
				userinfo.setRoleName(role.getRoleName());
				return userinfo;
			}
		}
		return null;
	}

	@Override
	public Pagination<HashMap<String,Object>> selectAllUsers(int page, int rows) {
		 String sql="select count(*) from user_info,role where user_info.roleID=role.id ";
		long total=baseDao.countSql(sql);
		Pagination<HashMap<String,Object>> pagination=new Pagination<HashMap<String,Object>>(page,(int) total, rows);
		List<HashMap<String,Object>>result=new ArrayList<HashMap<String,Object>>();
		sql="select user_info.id, user_info.userName,user_info.lastUpdateTime,user_info.userTel,user_info.roleID,role.roleName,role.description ,user_info.userCode from user_info,role where user_info.roleID=role.id ";
		 List<Object>  datas = baseDao.findSql(sql, page, pagination.getPageSize());
		 for(Object obj:datas){
			 if(obj instanceof Object[]){
				 Object[]temp=(Object[])obj;
				 HashMap<String,Object> map= new HashMap<String,Object>();
				 map.put("id", temp[0]);
				 map.put("userName", temp[1]);
				 map.put("lastUpdateTime", temp[2]);
				 map.put("userTel", temp[3]);
				 
				 map.put("roleID", temp[4]);
				 map.put("roleName", temp[5]);
				 map.put("description", temp[6]);
				 map.put("userCode", temp[7]);
				 result.add(map);
			 }
		 }
		pagination.setDatas(result);
		return pagination;
	}

	@Override
	public List<RoleModel> findAllRoleModel() {
		
		return roleDao.find("from RoleModel ");
	}

	@Override
	public UserInfo finishAddUserInfo(UserInfo user) {
		
		String hql="from UserInfo where userName=?";
		UserInfo  userinfo=baseDao.get(hql, user.getUserName());
		if(userinfo!=null){
			
			return null;
		}
		user.setLastUpdateTime(System.currentTimeMillis());
		String password=SysUtils.string2MD5(user.getPassword());
		user.setPassword(password);
		Serializable result= baseDao.save(user);
		 if(result!=null){
			 return user;
		 }
		 return null;
	}

	@Override
	public UserInfo findUserInfo(Integer uid) {
		
		return baseDao.get(UserInfo.class, uid);
	}

	@Override
	public UserInfo finishDelete(Integer uid) {
		UserInfo user=baseDao.get(UserInfo.class, uid);
		if(user!=null){
			baseDao.delete(user);
		}
		return user;
	}

	@Override
	public UserInfo finishUpdateUser(UserInfo userInfo) {
		UserInfo user=baseDao.get(UserInfo.class, userInfo.getId());
		if(user!=null){
			user.setLastUpdateTime(System.currentTimeMillis());
			if(!user.getPassword().equals(userInfo.getPassword())){
				String password=SysUtils.string2MD5(user.getPassword());
				user.setPassword(password);
			}
			user.setUserCode(userInfo.getUserCode());
			user.setUserTel(userInfo.getUserTel());
			user.setUserName(userInfo.getUserName());
			user.setRoleID(userInfo.getRoleID());
		}
		return user;
	}

	@Override
	public FeedBackModel finishAddFeeBack(FeedBackModel feedBackModel) {
		feedBackModel.setLastUpdateTime(System.currentTimeMillis());
		feedBackModel.setFeedTime(SysUtils.formatDate("yyyy-MM-dd HH:mm:ss"));
		feebackDao.save(feedBackModel);
		 return feedBackModel;
	}

	@Override
	public FeedBackModel findFeeBack(Integer uid) {
		return feebackDao.get(FeedBackModel.class, uid);
	}

	@Override
	public List<FeedBackModel> getFeeBacks() {
		return feebackDao.find("from FeedBackModel ");
	}

	@Override
	public FeedBackModel finishDeleteFeeBack(Integer uid) {
		FeedBackModel user=feebackDao.get(FeedBackModel.class, uid);
		if(user!=null){
			feebackDao.delete(user);
		}
		return user;
	}

	@Override
	public Pagination<HashMap<String, Object>> selectFeedBacks(int page, int rows) {
		 String sql="select count(*) from feed_back ";
			long total=baseDao.countSql(sql);
			Pagination<HashMap<String,Object>> pagination=new Pagination<HashMap<String,Object>>(page,(int) total, rows);
			List<HashMap<String,Object>>result=new ArrayList<HashMap<String,Object>>();
			sql="select feed_back.id,feed_back.content,feed_back.feedTime,feed_back.descript, lingjian.name,lingjian.bianHao, lingjian.xingHao from feed_back,lingjian where feed_back.lingJianId=lingjian.id ";
			 List<Object>  datas = baseDao.findSql(sql, page, pagination.getPageSize());
			 for(Object obj:datas){
				 if(obj instanceof Object[]){
					 Object[]temp=(Object[])obj;
					 HashMap<String,Object> map= new HashMap<String,Object>();
					 map.put("id", temp[0]);
					 map.put("content", temp[1]);
					 map.put("feedTime", temp[2]);
					 map.put("descript", temp[3]);
					 
					 map.put("name", temp[4]);
					 map.put("bianHao", temp[5]);
					 map.put("xingHao", temp[6]);
					 result.add(map);
				 }
			 }
			pagination.setDatas(result);
			return pagination;
	}

	@Override
	public List<UserInfo> getAllJianYanYuan() {
		return baseDao.find("from UserInfo where roleID=2");
	}

	@Override
	public List<UserInfo> getAllKeHus() {
		return baseDao.find("from UserInfo where roleID=3");
	}

	
}
